﻿using System;
using HISModels.ViewModel;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using KeanuMvc.Reference;
using HIS.Utils;
using System.Reflection;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using HISModels.Entities;
using System.Net;
using System.Configuration;

namespace HIS.Controllers.Workflow
{

    public class MasterTemplete : HIS.Controllers.AppBaseController
    {

        #region Fields
        private string strAction = string.Empty;
        private string strNotification = string.Empty;
        private HISModels.Entities.SysNavigationGroup sysNavigationGroup = null;
        private HISModels.Entities.SysNavigation sysNavigation = null;
        #endregion

        #region Workflow
        public MasterTemplete()
        {
            Data.Add("Pagetitle", "Workflow");
            Data.Add("Notification", strNotification);
        }

        [HIS.Utils.Securities.Authorization(RoleAction = "all")]
        public override void Index()
        {
            try
            {
                string ddr = GetWorkFlowTempletes();
                Data.Add("ddlWfTemplete", ddr);
                RenderFile("workflows\\mastertempletemaster.html");

            }
            catch (Exception ex)
            {
                HttpContext.Current.Response.Write(ex.Message);
            }
        }

        [HIS.Utils.Securities.Authorization(RoleAction = "all")]
        public void PurchaseRequisition()
        {
            try
            {
                string ddr = GetWorkFlowTempletes();

                Data["Pagetitle"] += " :: Create Workflow Document";
                Data.Add("ddlWfTemplete", ddr);


            }
            catch (Exception ex)
            {
                Data["Notification"] = Helper.ErrorMessage(ex.Message);
            }
            finally
            {
                RenderFile("Workflows\\createworkflow.html");
                base.RestService.Dispose();
            }
        }

        [HIS.Utils.Securities.Authorization(RoleAction = "all")]
        public void ViewApproval()
        {
            try
            {
            }
            catch (Exception ex)
            {
                Data["Notification"] = Helper.ErrorMessage(ex.Message);
            }
            finally
            {
                RenderFile("Workflows\\actionworkflow.html");
                base.RestService.Dispose();
            }
        }
        public string GetWorkFlowTempletes()
        {

            StringReader stringReaderDropDown = null;
            string strResult = string.Empty;
            string Name = string.Empty;
            string strUri = "";
            strUri = Global.ServiceUriDictionary["wfmastertemplate"] + Global.ServiceUriDictionary["requestformat"];
            try
            {
                stringReaderDropDown = base.RestService.GetJson(strUri);
                List<WorkflowMasterTemplate> listSp = JsonConvert.DeserializeObject<List<WorkflowMasterTemplate>>(stringReaderDropDown.ReadToEnd());
                List<object[]> activeArr = new List<object[]>();

                activeArr.Add(new object[] { 0, Name = "Default (Organisation)" });
                //activeArr.Add(new object[] { 1, Name = "Siloam Karawaci" });
                //activeArr.Add(new object[] { 2, Name = "Siloam Bonjer" });


                foreach (WorkflowMasterTemplate item in listSp) activeArr.Add(new object[] { item.Id, Name = item.NameValue });

                strResult = KeanuMvc.Reference.Html.DropDown("wfTemplete", string.Empty, activeArr, "");
                activeArr.Clear();
                activeArr = null;
            }
            catch
            {
            }
            finally
            {
                // stringReaderDropDown.Dispose();
            }
            return strResult;

        }
        [HIS.Utils.Securities.Authorization(RoleAction = "all")]
        public string GetListWorkFlowTempleteFromWorkFlowMasterTemplete()
        {
            string wfMasterTempleteId = HttpContext.Current.Request["wfmaster"].NullSafeToString();
            int wfmasterTempleteIdInt = 0;
            try
            {
                wfmasterTempleteIdInt = Convert.ToInt32(wfMasterTempleteId);
            }
            catch(Exception)
            {}
           
            string valueToReturn = "";
            switch (wfmasterTempleteIdInt)
            {
                case 0: valueToReturn = GetListManager();
                    break;
                default: throw new NotImplementedException("Belom ada tukang servisnya jonn!");

            }
            return valueToReturn;

            
        }

        public string GetListManager()
        {
            StringReader stringReaderDropDown = null;
            string strResult = string.Empty;
            string Name = string.Empty;
            string strUri = "";
            strUri = Global.ServiceUriDictionary["wfsuperior"] + 1 /*HIS.Utils.Helper.GetUserId()*/ + Global.ServiceUriDictionary["requestformat"];
            DataTablesNetGeneral<WorkflowTemplate> dt = new DataTablesNetGeneral<WorkflowTemplate>();
            try
            {
                stringReaderDropDown = base.RestService.GetJson(strUri);
                string temp = stringReaderDropDown.ReadToEnd();

                List<Employee> listSp = JsonConvert.DeserializeObject<List<Employee>>(temp);

                List<WorkflowTemplate> data = new List<WorkflowTemplate>();
                int i = 0;
                foreach (Employee e in listSp)
                {
                    i++;
                    WorkflowTemplate wt = new WorkflowTemplate();
                    wt.Employee = e;
                    wt.EmployeeId = e.Id;
                    wt.SeqOrder = i;
                    data.Add(wt);

                }
                
                
                dt.sEcho = 1;
                dt.iTotalRecords = listSp.Count;
                dt.iTotalDisplayRecords = listSp.Count;
                dt.aaData = data;
                dt.xIsThereError = false;





            }
            catch (Exception ex)
            {
                dt.sEcho = 1;
                dt.iTotalDisplayRecords = 0;
                dt.iTotalRecords = 0;
                dt.xErrorMessage = ex.Message;
                dt.xIsThereError = true;

            }

            return JsonConvert.SerializeObject(dt);


        }
        #endregion

    }

}